import os, string, regsub, time
import os, string, regsub, time
from chmod import chmod
import vcs
from vcs import TRUE, FALSE, NL, SYSTEM, WINDOWS, UNIX
from vcs import NULLDEV, DELIMITER
from vcs import is_readable, is_writable
import sys

#TEST=0         # no debugging
TEST=0         # trace function calls
#TEST=2         # for  debugging 
#TEST=3         # for HEAVY debugging 

gMod = "tcSettings:"
gTCSettingsDir = ""
gTCSettingsFile = "TeamConnectionSettings.sniff"
# build commmon options for TC command
def BuildCmdOptions(withSource, withRelative, withVerbose, debug=TEST):
		marker = "BuildCmdOptions"
		if debug > 0: 
			print "start", gMod+marker+NL,
		settings = ('','','','','','')
		#settings = ('','','','','')
		settings = readSettings()
		family = settings[0]
		user = settings[1]
		become = settings[2]
		rel = settings[3]
		area = settings[4]
		#relpath = settings[5]

		# if [ $area ];
		if 0!=len(area):
			track = "-workarea " + area
		else:
			track = ""
		# fi;
		if 0!=len(family):
			track = track + " -family " + family
		if 0!=len(become):
			track = track + " -become " + become
		if withVerbose:
			track = track + " -verbose"
		options = ""
		if (withSource == TRUE):
			options = options +   vcs.projectDir + "/" + vcs.workingFile
		if (withRelative == TRUE):
			options = options + " -release %s -relative %s %s"
			#args = (rel, relpath, track)
			args = (rel, vcs.privateRoot, track)
		else:
			options = options +  " -release %s %s"
			args = (rel, track)
		
		retVal = options % args
		if debug > 1:
			print "options = ",
			print options
		if debug > 0: 
			print "end", gMod+marker+NL,
		return retVal		

#read TeamConnection parameter from ini-file
def readSettings(debug=TEST):
	global gTCSettingsDir
	marker = "readSettings"
	if debug > 0: 
		print "start", gMod+marker+NL,
	retval = TRUE
	#look after HOME variable
	if os.environ.has_key("HOME"):
		gTCSettingsDir = os.environ["HOME"]
	else:
		print "(" + gMod + ")@BInformation:@B environment variable @BHOME@B not found ! Stop processing !"
		return -1
	if debug > 1: print marker + " gTCSettingsDir:", gTCSettingsDir
	#open settings file
	settingsFileName = vcs.joinpath(gTCSettingsDir, gTCSettingsFile)
	if debug > 1: print "settingsFileName: ", settingsFileName
	settingFile = open(settingsFileName, 'r')
	#read settings
	line = settingFile.readline()
	while line:
		if debug > 1: print "line: ", line
		tcParam = string.split(line,'=')
		tcParamKey = string.strip(tcParam[0])
		if debug > 1: print "tcParam: ", tcParam
		#find parameter and set value
		if tcParamKey == "TC_FAMILY":
			family = string.strip(tcParam[1])
		elif tcParamKey == "TC_USER":
			user = string.strip(tcParam[1])
		elif tcParamKey == "TC_BECOME":
			become = string.strip(tcParam[1])
		elif tcParamKey == "TC_RELEASE":
			release = string.strip(tcParam[1])
		elif tcParamKey == "TC_WORKAREA":
			workarea = string.strip(tcParam[1])
#		elif tcParamKey == "TC_RELPATH":
#			relpath = string.strip(tcParam[1])
		else:
			print gMod + marker + "Unknown TeamConnection setting parameter :("
		line = settingFile.readline()
	settingFile.close()

	if debug > 0: 
		print "end", gMod+marker+NL,
	#return (family,user,become,release,workarea,relpath)
	return (family,user,become,release,workarea)
	
#write TeamConnection parameter to ini-file
#def writeSettings(family, user, become, release, workarea, relpath, debug=TEST):
def writeSettings(family, user, become, release, workarea, debug=TEST):
	global gTCSettingsDir,gTCSettingsFile
	marker = "writeSettings"
	if debug > 0: 
		print "start", gMod+marker+NL,
	#make settings text file
	settings = ""
	setLine = "TC_FAMILY=" + family + NL
	settings = settings + setLine
	setLine = "TC_USER=" + user + NL
	settings = settings + setLine
	setLine = "TC_BECOME=" + become + NL
	settings = settings + setLine
	setLine = "TC_RELEASE=" + release + NL
	settings = settings + setLine
	setLine = "TC_WORKAREA=" + workarea + NL
	settings = settings + setLine
#	setLine = "TC_RELPATH=" + relpath + NL
#	settings = settings + setLine
	if debug > 1: print marker, "settings: ", settings
	#wite settings file
	settingsFileName = vcs.joinpath(gTCSettingsDir, gTCSettingsFile)
	settingFile = open(settingsFileName, 'w').write(settings)
	if debug > 0: 
		print "end", gMod+marker+NL,
	return
	
#start point
#this method will be called by gModul reloading
#def start(debug=TEST):
#	marker = " start"
#	return readSettings()

#if start() == -1:
#	exit(1)

if __name__ == "__main__":
	import TCReload
	TCReload.ReloadAll()
	pass
